//
//  ZLFHomeViewController.swift
//  ZLFXYF_Swift
//
//  Created by zluof on 16/12/5.
//  Copyright © 2016年 zluof. All rights reserved.
//

import UIKit

class ZLFHomeViewController: ZLFViewController,UITableViewDelegate,UITableViewDataSource {

    var hearView : UIView?
    var titles : [String] = [String]()
    var selectBtn : UIButton?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.title = "首页"
        self.view.addSubview(self.tableV)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    lazy var tableV :UITableView = {
       let tableview = UITableView.init(frame: self.view.bounds, style:.plain)
        tableview.delegate = self
        tableview.dataSource = self
        let hearView = UIView(frame: CGRect.init(x: 0, y: 0, width: SWIDTH, height: 150))
        hearView.layer.contents = (UIImage(named: "index_banner"))?.cgImage
        tableview.tableHeaderView = hearView
        return tableview
    }()
    lazy var indicatyorView :UIView = {
        let indicatorView = UIView()
        indicatorView.backgroundColor = MYRGBCOLOR(67, 183, 246)
        indicatorView.height = 2
        indicatorView.y = (self.hearView?.height)! - indicatorView.height
        print(self.hearView!.height - 2)
        return indicatorView
    }()
  
}


// MARK:-tableview代理
extension ZLFHomeViewController{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identfier : String = "cellIdentfier"
        var cell = tableView.dequeueReusableCell(withIdentifier: identfier)
        if cell == nil{
            cell = ZLFClosethCell.init(style: .default, reuseIdentifier: identfier)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let viewHear = UIView(frame: CGRect.init(x: 0, y: 120, width: SWIDTH, height: 50))
        viewHear.backgroundColor = UIColor.white
        self.hearView = viewHear
        
        
        
        self.titles = ["衣服类","鞋靴类","家纺类"];
        let widthBtn : CGFloat = SWIDTH/CGFloat(self.titles.count)
        let heightBtn : CGFloat = self.hearView!.frame.size.height;
        var i = 0
        for title in self.titles {
            let btn = UIButton()
            btn.tag = i
            btn.frame = CGRect.init(x: CGFloat(i) * widthBtn, y: 0, width: widthBtn, height: heightBtn)
            btn.setTitle(title, for: UIControlState())
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.setTitleColor(THEMECOLOR, for: .disabled)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
            btn.addTarget(self, action:#selector(hearTitleClicked(titleBtn:)), for: .touchUpInside)
            self.hearView!.addSubview(btn)
            if i == 0  {
                btn.isEnabled = false
                self.selectBtn = btn
                btn.titleLabel!.sizeToFit()
                self.indicatyorView.frame = CGRect.init(x: btn.titleLabel!.centerX, y: (self.hearView?.height)! - self.indicatyorView.height, width: btn.titleLabel!.width + 30, height: 2)
            }
            i += 1
        }
        self.hearView!.addSubview(self.indicatyorView)
        
        return self.hearView
    }
    
    func hearTitleClicked(titleBtn: UIButton) -> Void {
        self.selectBtn?.isEnabled = true;
        titleBtn.isEnabled = false
        self.selectBtn = titleBtn;
        
        UIView.animate(withDuration: 0.2) {
            let index : CGFloat =  CGFloat(titleBtn.tag)
            self.indicatyorView.width = (titleBtn.titleLabel!.width)
            self.indicatyorView.x = (titleBtn.titleLabel!.centerX) * index;
            self.indicatyorView.frame = CGRect.init(x: self.indicatyorView.x, y: (self.hearView?.height)! - self.indicatyorView.height, width: self.indicatyorView.width, height: 2)
        }
        
    }

}
